


let BaseAnimView = require('./baseAnimView');

const templateDiv = `
    <label>相对旋转</label><br/>
    <label>角度</label>
    <input class="angle mini_input" type="number">
    <label>时间</label>
    <input class="time mini_input" type="number" step="0.02" min="0.02"><br/>
    <button class="btnPlay">播放</button>
    <button class="btnStop">停止</button>
    <button class="btnDelete">删除</button>
    <label class="info"></label>
`;

let doubleBind = require('../../doubleBind');
let settings = require('./../../settings');
let animUtils = require('./animUtils');

class RotateByView extends BaseAnimView{
    constructor(){
        super(templateDiv);

        // this.rootEl = document.createElement('div');
        // this.rootEl.innerHTML = templateDiv;
        // this.lbInfo = this.rootEl.querySelector('.info');
        //
        // // 播放
        // let btnPlay = this.rootEl.querySelector('.btnPlay');
        // btnPlay.addEventListener('click',()=>{
        //     // TODO:是否需要判断正在动画??? 不需要,点击后 重新创建新的动画取代之前的
        //     //
        //     let selectedSp = editor.getInspectView().getSelectedSp();
        //     if(selectedSp){
        //         this._playAnim(selectedSp);
        //
        //         // 动画的时候 隐藏
        //         editor.gameApp.hideSelectedFlag();
        //     }
        // },false);
        //
        // // 停止
        // let btnStop = this.rootEl.querySelector('.btnStop');
        // btnStop.addEventListener('click',()=>{
        //     let selectedSp = editor.getInspectView().getSelectedSp();
        //     if(selectedSp){
        //         editor.gameApp.actionMgr.removeAllActionOnTarget(selectedSp);
        //         editor.getInspAnimView().restoreInitData();
        //         editor.gameApp.selectDeco(selectedSp);
        //         this.updateInfoLabel();
        //     }
        // },false);
        //
        // // 删除
        // let btnDelete = this.rootEl.querySelector('.btnDelete');
        // btnDelete.addEventListener('click',()=>{
        //     if(settings.showConfirmDialog('确认要删除动画么?')){
        //         // 动画的时候 隐藏
        //         let selectedSp = editor.getInspectView().getSelectedSp();
        //         if(selectedSp){
        //             let inspectModel =  editor.getInspectView().getModel();
        //             editor.gameApp.actionMgr.removeAllActionOnTarget(selectedSp);
        //             editor.getInspAnimView().restoreInitData();
        //
        //             // 删除动画数据
        //             selectedSp.anims = null;
        //             inspectModel.anims = null;
        //
        //             editor.gameApp.selectDeco(selectedSp);
        //         }
        //         this.destroy();
        //     }
        // },false);
        //
        // this.bindModel();
        // this.reset();
    }

    bindModel(){
        let self = this;
        let lst = [];
        lst.push({
            el:this.rootEl.querySelector('.angle'),
            name:'angle',
            domName:'input',
            bNumber:true,
            callback:function () {
                let model = editor.getInspectView().getModel();
                if(model.anims){
                    model.anims.dx = self.model.angle;
                }
            }
        });
        lst.push({
            el:this.rootEl.querySelector('.time'),
            name:'time',
            domName:'input',
            bNumber:true,
            callback:function () {
                let model = editor.getInspectView().getModel();
                if(model.anims){
                    model.anims.time = self.model.time;
                }
            }
        });

        this.model = {};
        lst.forEach(item=>{
            doubleBind(this.model,item);
        });
    }

    reset(){
        /**
         * 表示 动画 rotateBy 角度30 会耗费 1s
         * 那么 整个动画过程就是:
         *  RepectForever(Sequence(A->B->A)
         *  其中 A->B 的时间就是 1s
         *
         */
        this.model.angle = 15;
        this.model.time = 2;
    }

    _playAnim(selectSp){

        let inspAnimView = editor.getInspAnimView();
        inspAnimView.saveInitData();

        // 1. 删除之前的
        let actionMgr = cc.director.getActionManager();
        actionMgr.removeActionByTag(G.Tags.DecoBaseAction,selectSp.node);

        // 2. 创建动画,并进行播放
        let time = this.model.time;
        let angle = this.model.angle;
        let forever = animUtils.makeRotateBy(time,angle);
        selectSp.node.runAction(forever);
        this.updateInfoLabel();
    }

    // updateInfoLabel(){
    //     let selectSp = editor.getInspectView().getSelectedSp();
    //     if(selectSp){
    //         let actionMgr = cc.director.getActionManager();
    //         let action =  actionMgr.getActionByTag(G.Tags.DecoBaseAction,selectSp.node);
    //         if(action){
    //             this.lbInfo.textContent = '动画中.....';
    //             this.lbInfo.style.color = 'red';
    //             return;
    //         }
    //     }
    //
    //     this.lbInfo.textContent = '停止';
    //     this.lbInfo.style.color = 'black';
    // }
}

module.exports = RotateByView;
